package com.das.mechanic_base.mvp.view.create.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.base.X3BasePresenter;
import com.das.mechanic_base.base.X3BaseViewPagerLazyFragment;
import com.das.mechanic_base.bean.create.SelectWorkBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3ViewPagerBind;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.WrapPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import butterknife.BindView;

import static androidx.fragment.app.FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

public class X3ServiceSelectDetectionFragment extends X3BaseViewPagerLazyFragment {

    @BindView(R2.id.tb_layout)
    MagicIndicator tb_layout;
    @BindView(R2.id.vp_main)
    ViewPager vp_main;

    private SelectWorkBean workBean;

    private X3ServicePagerListAdapter listAdapter;

    public boolean isDetection;

    @Override
    protected int getLayoutId() {
        return R.layout.x3_service_select_detection_fragment;
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    public void getBundle(Bundle bundle) {
        workBean = (SelectWorkBean) bundle.getSerializable("workBean");
        isDetection = bundle.getBoolean("isDetection");
    }

    @Override
    protected void lazyInit() {
        listAdapter = new X3ServicePagerListAdapter(getChildFragmentManager(),BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        vp_main.setAdapter(listAdapter);
        listAdapter.isDetection = isDetection;
        listAdapter.changeServicePager(workBean.shopCategoryEntityList);
        initTabLayout();
    }

    public static X3ServiceSelectDetectionFragment newInstance(SelectWorkBean workBean,boolean isDetection) {
        X3ServiceSelectDetectionFragment detectionFragment = new X3ServiceSelectDetectionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("workBean",workBean);
        bundle.putBoolean("isDetection", isDetection);
        detectionFragment.setArguments(bundle);
        return detectionFragment;
    }

    @Override
    protected X3BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void everyInit() {

    }

    private void initTabLayout() {
        CommonNavigator tbNavigator = new CommonNavigator(getContext());
        tbNavigator.setScrollPivotX(0.35f);
        tbNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return workBean == null ? 0 : workBean.shopCategoryEntityList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(workBean.shopCategoryEntityList.get(index).name);
                simplePagerTitleView.setNormalColor(Color.parseColor("#707380"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#333333"));
                simplePagerTitleView.setOnClickListener(v -> vp_main.setCurrentItem(index));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                WrapPagerIndicator indicator = new WrapPagerIndicator(context);
                indicator.setRoundRadius(X3ScreenUtils.dipToPx(6,context));
                indicator.setHorizontalPadding(X3ScreenUtils.dipToPx(12,context));
                indicator.setVerticalPadding(X3ScreenUtils.dipToPx(8,context));
                indicator.setFillColor(Color.parseColor("#f2f2f5"));
                return indicator;
            }
        });
        tb_layout.setNavigator(tbNavigator);
        X3ViewPagerBind.bind(tb_layout,vp_main);
    }
}
